package com.cmict.data.form.api.util;

import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @description: sql拼接工具类
 * @author: qiaoyu
 * @create: 2022-03-15 22:57
 **/
public class FileUtil {


	public static Map<String,String> fileUpload(MultipartFile file, String uploadFolder) {
		Map<String,String> map = new HashMap<>();
		//file.getSize()>1048576
		if (file.isEmpty()) {
			System.out.println("文件为空");
		}
		String fileName = file.getOriginalFilename();  // 文件名
		String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
		//String filePath = uploadUrl + uploadFolder; // 上传后的路径
		String newFileName = UUID.randomUUID() + suffixName; // 新文件名
		File dest = new File(uploadFolder + newFileName);
		if (!dest.getParentFile().exists()) {
			dest.getParentFile().mkdirs();
		}
		try {
			file.transferTo(dest);
		} catch (Exception e) {
			e.printStackTrace();
		}
		map.put("name",fileName);
		map.put("url",uploadFolder + newFileName);
		return map;
	}

	/**
	 * 把单元格的内容转为字符串
	 *
	 * @param xssfCell 单元格
	 * @return String
	 */
	public static String getString(XSSFCell xssfCell) {
		if (xssfCell == null) {
			return "";
		}
		if (xssfCell.getCellTypeEnum() == CellType.NUMERIC) {
			return String.valueOf(xssfCell.getNumericCellValue());
		} else if (xssfCell.getCellTypeEnum() == CellType.BOOLEAN) {
			return String.valueOf(xssfCell.getBooleanCellValue());
		} else {
			return xssfCell.getStringCellValue();
		}
	}
}

